package main

import (
	"log"

	"data_service/db"
	"data_service/models"
	"data_service/router"
	"data_service/server/websocket"
)

func main() {
	// 初始化数据库
	dbInstance := db.InitDB()
	models.SetDB(dbInstance)

	// 初始化WebSocket服务器
	wsServer := websocket.NewWebSocketServerInstance("0.0.0.0:8080") // WebSocket独立端口

	r := router.SetupRouter(wsServer)

	// 将路由设置到WebSocket服务器
	wsServer.SetHandler(r)

	// 启动WebSocket服务器
	go func() {
		if err := wsServer.Start(); err != nil {
			log.Fatalf("Failed to start WebSocket server: %v", err)
		}
	}()

	go func() {
		// HTTP服务端口
		// 启动HTTP服务器
		log.Println("Starting HTTP server on :8081")
		if err := r.Run(":8081"); err != nil {
			log.Fatalf("Failed to start HTTP server: %v", err)
		}
	}()

	// 设置信号处理器，优雅关闭服务
	websocket.SetupSignalHandler(wsServer)
}
